package com.example.wx.dailyreport.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.wx.dailyreport.pojo.Authority;
import com.example.wx.dailyreport.service.AuthorityService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hulinbo
 * @since 2020-07-30
 */
@RestController
@RequestMapping("/authority")
public class AuthorityController {
    @Autowired
    private AuthorityService authorityService;

    @GetMapping("/getallauthority")
    public Map<String, Object> getAllAuthority() {
        Map<String, Object> map = new LinkedHashMap<>();
        List<Authority> list = authorityService.list();
        try {
            map.put("status", 200);
            map.put("data", list);
        } catch (Exception e) {
            map.put("status", 400);
        }
        return map;
    }

    @GetMapping("/getauthoritybyroleids")
    public Map<String, Object> getAuthorityByRoleIds(int[] ids) {
        Map<String, Object> map = new LinkedHashMap<>();
        Set<String> set = null;
        try {
            if (ArrayUtils.isNotEmpty(ids))
                set = authorityService.selectAuthorityByRoleIds(ids);
            map.put("status", 200);
            map.put("data", set);
        } catch (Exception e) {
            map.put("status", 400);
        }
        return map;
    }

    @GetMapping("/getauthorityinpageby_")
    public Map<String, Object> getAuthorityInPageBy_(Authority authority, @RequestParam(defaultValue = "1") int page, int limit) {
        Map<String, Object> map = new LinkedHashMap<>();
        IPage<Authority> iPage = authorityService.selectAuthorityInPage(authority, page, limit);
        map.put("status", 200);
        map.put("data", iPage.getRecords());
        map.put("count", iPage.getTotal());
        return map;
    }

    @PostMapping("/insertauthority")
    public Map<String, Object> insertAuthority(Authority authority) {
        Map<String, Object> map = new LinkedHashMap<>();
        if (authorityService.insertAuthority(authority))
            map.put("status", 200);
        else
            map.put("status", 400);
        return map;
    }

    @PostMapping("/updateauthority")
    public Map<String, Object> updateAuthority(Authority authority) {
        Map<String, Object> map = new LinkedHashMap<>();
        if (authorityService.updateById(authority))
            map.put("status", 200);
        else
            map.put("status", 400);
        return map;
    }

    @PostMapping("/deleteauthority")
    public Map<String, Object> deleteAuthority(Integer id) {
        Map<String, Object> map = new LinkedHashMap<>();
        if (authorityService.deleteAuthorityById(id))
            map.put("status", 200);
        else
            map.put("status", 400);
        return map;
    }

}

